package megrez.job.custom.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import megrez.job.common.Snowflake;

/**
 * 日志
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@Table(name = "job_log")
@EntityListeners({ AuditingEntityListener.class })
public class Log {
    @Id
    @EqualsAndHashCode.Include
    @Column(updatable = false, nullable = false, unique = true)
    private String id;

    @Version
    @Column(nullable = false)
    private Integer version;

    @CreatedDate
    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;

    @ManyToOne
    @JoinColumn(name = "configId")
    private Config config;

    /** 配置快照 */
    private String configSnapshot;

    /** 类型 */
    @Enumerated
    private TaskState state;

    /** 消息 */
    private String message;

    /** 耗时：毫秒 */
    private Long costTime;

    public Log() {
    }

    public Log(Config config) {
        this.config = config;
    }

    @PrePersist
    public void preAction() {
        if (getId() == null) {
            setId(Snowflake.nextIdStr());
        }

    }

}
